using System;
using System.Reflection;
using OrangeProject.ReactiveList.Core.Client;
using OrangeProject.ReactiveList.Core.Communication;

namespace ReactiveListUnitTest.Entities
{
    public class MyDataContext : DataContext
    {
        public ReactiveList<Name> Names { get; set; }
        public ReactiveList<Person> Persons { get; set; }
        public ReactiveList<Company> Companies { get; set; }
        public ReactiveList<Contacts> Contactses { get; set; }

        public MyDataContext(ITransportCore transportCore) : base(transportCore)
        {
            Names = new ReactiveList<Name>(this, list => new Name {DataContext = this}, "Names", (oldName, newName) =>
            {
                oldName.En = newName.En;
                oldName.Ug = newName.Ug;
                oldName.Zh = newName.Zh;
            });
            Lists.Add(Names);

            Persons = new ReactiveList<Person>(this, list => new Person {DataContext = this}, "Persons",(oldPerson, newPerson) =>
                {
                    Console.WriteLine(newPerson.Contacts);
                    oldPerson.Name = newPerson.Name;
                    oldPerson.Company = newPerson.Company;
                    oldPerson.Contacts = newPerson.Contacts;
                    oldPerson.Age = newPerson.Age;
                    oldPerson.Addr = newPerson.Addr;
                    oldPerson.IsBoy = newPerson.IsBoy;
                });
            Lists.Add(Persons);
            
            Companies = new ReactiveList<Company>(this, list => new Company {DataContext = this}, "Companies",
                (oldCompany, newCompany) =>
                {
                    oldCompany.Name = newCompany.Name;
                    oldCompany.Persons = newCompany.Persons;
                    oldCompany.Contacts = newCompany.Contacts;
                    oldCompany.Address = newCompany.Address;
                });
            Lists.Add(Companies);
            
            Contactses = new ReactiveList<Contacts>(this, list => new Contacts {DataContext = this}, "Contacts",
                (oldVal, newVal) =>
                {
                    oldVal.EMail = newVal.EMail;
                    oldVal.WeChat = newVal.WeChat;
                    oldVal.PhoneNumber = newVal.PhoneNumber;
                });
            Lists.Add(Contactses);
        }
    }
}